package com.beilang.hotline.title.dao;

import com.beilang.base.core.dao.PaginationBaseDAO;
import com.beilang.base.util.StringUtil;
import com.beilang.base.pagination.PaginationHelper;
import com.beilang.hotline.title.model.TitleModel;
import com.beilang.hotline.title.to.SearchTO;

import java.util.Map;
import java.util.HashMap;
import java.util.List;


public class TitleDAO  extends PaginationBaseDAO {

    public TitleModel createTitle(TitleModel titleModel) {
        titleModel.setDeleteInd("N");
        return (TitleModel)this.save(titleModel);
	}

	public TitleModel saveTitle(TitleModel titleModel) {
		return (TitleModel)this.save(titleModel);
	}

	public void deleteTitle(Long titleId){
		TitleModel title= this.getTitle(titleId);
        title.setDeleteInd("Y");
        this.save(title);
	}

	public TitleModel getTitle(Long titleId){
		return (TitleModel)this.get(TitleModel.class, titleId);
	}

	public Integer searchCount(SearchTO searchTO) {
        Map map = new HashMap();
        if(searchTO.getSysId()!=null){
        	map.put("sysId", searchTO.getSysId());
        }
        if(searchTO.getIssueId()!=null){
        	map.put("issueId", searchTO.getIssueId());
        }
        String title= StringUtil.convertEmpty2Null(searchTO.getTitle());
        if(title!=null)title="%"+title+"%";
        map.put("title", title);
        return (Integer) getSqlMapClientTemplate().queryForObject("searchTitleCount", map);
    }

    public List searchList(PaginationHelper helper, SearchTO searchTO) {
        Map map = new HashMap();
        if(searchTO.getSysId()!=null){
        	map.put("sysId", searchTO.getSysId());
        }
        if(searchTO.getIssueId()!=null){
        	map.put("issueId", searchTO.getIssueId());
        }
        String title= StringUtil.convertEmpty2Null(searchTO.getTitle());
        if(title!=null)title="%"+title+"%";
        map.put("title", title);
        map = constructPaginationMap(helper, map);
        return getSqlMapClientTemplate().queryForList("searchTitleList", map);
    }

    protected  String mapSortField(String dataField){
    	if(StringUtil.isNullOrBlank(dataField))return dataField;
    	if(dataField.equalsIgnoreCase("sysName")) return "system_id";
    	if(dataField.equalsIgnoreCase("issueName")) return "issue_id";
        if(dataField.equalsIgnoreCase("title")) return "title";
    	return dataField;
    }

    public void deleteAllSysTitles(Long sysId){
        Map map = new HashMap();
        map.put("sysId", sysId);
        getSqlMapClientTemplate().update("deleteAllSysTitles",map);
    }

    public void deleteAllModuleTitles(Long moduleId) {
        Map map = new HashMap();
        map.put("issueId", moduleId);
        getSqlMapClientTemplate().update("deleteAllIssueTitles",map);
    }

}
